﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC4Shop.Models;
using MVC4Shop.dao;
using System.IO;

namespace MVC4Shop.Controllers
{
    public class CategoryController : Controller
    {
        //
        // GET: /Category/

        public ActionResult Index()
        {
            
            return View(CategoryDAO.GetAll());
        }

        //
        // GET: /Category/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Category/Create

        public ActionResult Create()
        {

            return View();
        }

        //
        // POST: /Category/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection, HttpPostedFileBase file)
        {
            try
            {
                // TODO: Add insert logic here
                CategoryModel model = new CategoryModel();
                model.name = collection["name"].ToString();
                model.image = "images/" +file.FileName.ToString();
                if (file.ContentLength > 0)
                {
                    String fileName = Path.GetFileName(file.FileName);
                    String path = Path.Combine(Server.MapPath("~/images/"), file.FileName);
                    file.SaveAs(path);
                }
                CategoryDAO.Insert(model);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Category/Edit/5

        public ActionResult Edit(int id)
        {
            List<CategoryModel> list =  CategoryDAO.GetById(id.ToString());
            CategoryModel model = list[0];
            return View(model);
        }

        //
        // POST: /Category/Edit/5

        [HttpPost]
        public ActionResult Edit(CategoryModel model , FormCollection collection, HttpPostedFileBase file)
        {
            try
            {
                // TODO: Add update logic here
                if (file!=null)
                {
                    String path = Path.Combine(Server.MapPath("~/"), model.image);
                    file.SaveAs(path);
                    CategoryDAO.Edit(model);
                }
                else
                {
                    CategoryDAO.Edit(model);
                }
                

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Category/Delete/5

        public ActionResult Delete(int id)
        {

            return View();
        }

        //
        // POST: /Category/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                CategoryDAO.Delete(id.ToString());
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
